package com.architecture.core.service.associate;

import com.architecture.core.entity.Category;
import com.architecture.core.entity.Permission;
import com.architecture.core.entity.PermissionRole;
import com.architecture.core.except.ErrException;
import com.architecture.core.service.AbstractService;
import com.architecture.core.utils.ErrCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: aguang
 * Date: 13-1-21
 * Time: 下午6:13
 * To change this template use File | Settings | File Templates.
 */
@Service("manageCategoryService")
public class CategoryAssociateService extends AbstractService {

    public void associatePermission(String categoryId,String permissionId)throws ErrException {
        if (StringUtils.isBlank(categoryId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        if (StringUtils.isBlank(permissionId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        Category category = (Category) super.baseDAO.findUnique(Category.class,categoryId);

        Permission permission = (Permission) super.baseDAO.findUnique(Permission.class, permissionId);

        category.getPermissionList().add(permission);
        permission.setCategory(category);

        super.baseDAO.update(category);
        super.baseDAO.update(permission);

    }

    public void associatePermission(String categoryId,List<String> permissionIdList)throws ErrException {
        if (CollectionUtils.isEmpty(permissionIdList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        for (String permissionId : permissionIdList) {
            this.associatePermission(categoryId,permissionId);
        }

    }

    public void deassociatePermission(String categoryId,String permissionId)throws ErrException {

        if (StringUtils.isBlank(categoryId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        if (StringUtils.isBlank(permissionId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        Category category = (Category) super.baseDAO.findUnique(Category.class, categoryId);
        Permission permission = (Permission) super.baseDAO.findUnique(Permission.class, permissionId);

        category.getPermissionList().remove(category);
        permission.setCategory(null);

        super.baseDAO.update(category);
        super.baseDAO.update(permission);

    }

    public void deassociatePermission(String categoryId,List<String> permissionIdList)throws ErrException{

        if (CollectionUtils.isEmpty(permissionIdList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        for (String permissionId : permissionIdList) {
            this.deassociatePermission(categoryId,permissionId);
        }
    }

    public void associatePermissionRole(String categoryId,String permissionRoleId)throws ErrException{

        if (StringUtils.isBlank(categoryId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        if (StringUtils.isBlank(permissionRoleId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        Category category = (Category) super.baseDAO.findUnique(Category.class,categoryId);

        PermissionRole permissionRole = (PermissionRole) super.baseDAO.findUnique(PermissionRole.class, permissionRoleId);

        category.getPermissionRoleList().add(permissionRole);
        permissionRole.setCategory(category);

        super.baseDAO.update(category);
        super.baseDAO.update(permissionRole);
    }

    public void associatePermissionRole(String categoryId,List<String> permissionRoleIdList)throws ErrException{
        if (CollectionUtils.isEmpty(permissionRoleIdList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        for (String permissionRoleId : permissionRoleIdList) {
            this.associatePermissionRole(categoryId,permissionRoleId);

        }

    }

    public void deassociatePermissionRole(String categoryId,String permissionRoleId)throws ErrException {

        if (StringUtils.isBlank(categoryId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        if (StringUtils.isBlank(permissionRoleId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        Category category = (Category) super.baseDAO.findUnique(Category.class, categoryId);
        PermissionRole permissionRole = (PermissionRole) super.baseDAO.findUnique(PermissionRole.class, permissionRoleId);

        category.getPermissionRoleList().remove(category);
        permissionRole.setCategory(null);

        super.baseDAO.update(category);
        super.baseDAO.update(permissionRole);
    }

    public void deassociatePermissionRole(String categoryId,List<String> permissionRoleIdList)throws ErrException{
        if (CollectionUtils.isEmpty(permissionRoleIdList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        for (String permissionRoleId : permissionRoleIdList) {
            this.deassociatePermissionRole(categoryId,permissionRoleId);

        }
    }
}
